//
//  LGResultListViewController.m
//  LuxuryGoods
//
//  Created by 纳里健康 on 2018/5/29.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGResultListViewController.h"
#import "LGDetailCollectionViewCell.h"
#import "LGGoodsModel.h"


@interface LGResultListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *productDataArray;

@end

@implementation LGResultListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.productDataArray = [NSMutableArray array];
    
    [self makeColocationView];
    
    [self requestData];
}

- (void)requestData
{
    NSLog(@"%lu",(unsigned long)self.Type);
    
    NSInteger typeNumber = 0;
    
    switch (self.Type) {
        case synthesizeType:
            typeNumber = 0;
            break;
        case priceupType:
            typeNumber = 1;
            break;
        case priceDowmType:
            typeNumber = 2;
            break;
        case renovateType:
            typeNumber = 10;
            break;
        case renovateDownType:
            typeNumber = 11;
            break;
            
        default:
            break;
    }
    
    NSDictionary *dict = @{
                           @"keywords":[self.keyWords isNotBlank] ? self.keyWords : @"",
                           @"sort":[NSNumber numberWithInteger:typeNumber],
                           };
    
    [[MainApi sharedInstance] getPath:@"Goods/getList" params:dict resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            
            NSDictionary *dict = responseObject[@"data"];
            
            NSArray *goodArray = dict[@"goods_list"];
            
            for (NSDictionary *goodDict in goodArray) {
                
                LGGoodsModel *model = [LGGoodsModel yy_modelWithJSON:goodDict];
                
                [self.productDataArray addObject:model];
            }
            
            [self.collectionView reloadData];
        }
        
    }];

    
}

- (void)makeColocationView
{
    CGFloat optionY = 0;
    CGFloat collectionHeight = SCREEN_HEIGHT;

    if(self.fromType == typeBestSeller)
    {
        optionY = (SCREEN_WIDTH / 72) * 35;

        collectionHeight = SCREEN_HEIGHT  - 40 - optionY - NAVIGATOR_HEIGHT;
        
    }else if(self.fromType == typeSearch)
    {
        optionY = 0;

        collectionHeight = SCREEN_HEIGHT - NAVIGATOR_HEIGHT - 40;
    }
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 5;
    
    flowLayout.minimumInteritemSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, collectionHeight) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = UIColorFromRGB(0xEFF1F1);
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGDetailCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"LGDetailCollectionViewCell"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"LGDetailCollectionViewCell";
    
    LGDetailCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    [cell setGoodCellWithGoodModle:self.productDataArray[indexPath.row]];
    
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = ceilf(self.collectionView.width - 5)/2;
    
    return  CGSizeMake(itemWidth, 237);
}

#pragma mark  定义整个CollectionViewCell的四个边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 5, 0);//（上、左、下、右）
}

//#pragma mark -- 返回头部视图
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if(self.fromType == typeBestSeller)
//    {
//        if([kind isEqualToString:UICollectionElementKindSectionHeader])
//        {
//            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
//
//            UIImageView *bannerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH / 72) * 35)];
//
//            bannerImage.image = [UIImage imageNamed:@"3"];
//
//            [header addSubview:bannerImage];
//
//            return header;
//        }
//        return nil;
//    }
//
//    return nil;
//}
//
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//
//    return  self.fromType == typeBestSeller ? CGSizeMake(SCREEN_WIDTH, (SCREEN_WIDTH / 72) * 35) : CGSizeMake(0, 0);
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
